package com.situ.stl.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Admin;

import com.situ.stl.mapper.AdminMapper;
import com.situ.stl.service.AdminService;
import com.situ.stl.util.Result;
import com.situ.stl.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getAdminByPhone(String phone) {
        return adminMapper.selectAdminByPhone(phone);
    }

    @Override
    public List<Admin> select(Admin admin) {
        return adminMapper.select(admin);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Admin admin) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 10;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<Admin> admins = adminMapper.select(admin);
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return pageInfo;
    }

    @Override
    public int addAdmin(Admin admin) {
         //查看手机号是否重复
        Admin dbAdmin = adminMapper.selectAdminByPhone(admin.getPhone());
        if (!ObjectUtils.isEmpty(dbAdmin)){
            return Constants.PHONE_HAS_ERROR;
        }
        //添加管理员信息
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        admin.setPassword(md5Password);
        return  adminMapper.insert(admin);

    }

    @Override
    public int modifyAdmin(Admin admin) {
        Admin dbAdmin = adminMapper.selectAdminByPhone(admin.getPhone());
        if (!ObjectUtils.isEmpty(dbAdmin) && admin.getId() != dbAdmin.getId()){
            return Constants.PHONE_HAS_ERROR;
        }
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        if (!ObjectUtils.isEmpty(dbAdmin) &&!md5Password.equalsIgnoreCase(dbAdmin.getPassword())){
            return Constants.PASSWORD_ERROR;
        }
        if (StringUtils.hasText(admin.getNewPassword())){
            String md5Password1 = getMd5Password(admin.getNewPassword(), admin.getPhone());
            admin.setPassword(md5Password1);
        }

        return  adminMapper.modifyAdmin(admin);

    }

    @Override
    public int deleteAdmin(Integer id) {
        if(id == 1){
            return -1;
        }
        return adminMapper.delete(id);
    }

    @Override
    public Admin login(Admin admin) throws Exception {
        // 判断账号，密码格式是否正确
        if (admin.getPhone() == null || admin.getPhone().length() < 3 || admin.getPhone().length() > 16) {
            throw new Exception("账号格式不正确");
        }

        if (admin.getPassword() == null || admin.getPassword().length() < 3 || admin.getPassword().length() > 16) {
            throw new Exception("密码格式不正确");
        }
        //手机号是否存在
        Admin admin1 = adminMapper.selectAdminByPhone(admin.getPhone());
        if (ObjectUtils.isEmpty(admin1)){
            throw new Exception("手机号或密码错误");
        }
        //对输入的密码加密后进行密码比对
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        if(!md5Password.equals(admin1.getPassword())){
            throw new Exception("手机号或密码错误");

        }



        return admin1;
    }

    @Override
    public Admin getByPhone(String phone) {
        return adminMapper.selectAdminByPhone(phone);
    }

    @Override
    public int edge(Admin admin) {
        Admin dbAdmin = adminMapper.selectAdminByPhone(admin.getPhone());
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        if (!md5Password.equalsIgnoreCase(dbAdmin.getPassword())){
            return Constants.PASSWORD_ERROR;
        }
        return Constants.STATUS_200;
    }

    //使用md5对密码进行盐值加密
    private  String getMd5Password(String password, String salt) {
        //三次加密
        for (int i = 0; i < 3; i++) {
            String newPs = salt + password + salt;
            //md5的16进制方式加密
            password = DigestUtils.md5DigestAsHex(newPs.getBytes()).toUpperCase();
        }
        return password;
    }
}
